package com.qingge.springboot.service.impl;

import com.qingge.springboot.controller.dto.LikedCountDTO;
import com.qingge.springboot.entity.UserLike;
import com.qingge.springboot.service.RedisService;
import com.qingge.springboot.utils.RedisKeyUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class RedisServiceImpl implements RedisService {

    @Autowired
    RedisTemplate redisTemplate;

//    @Autowired
//    IUserLikeService likedService;

    @Override
    public boolean GetLikeFromRedis(Integer likedMessageId, Integer likedPostId) {
        String str=likedMessageId.toString()+"::"+likedPostId.toString();
        Object a= redisTemplate.opsForHash().get(RedisKeyUtils.MAP_KEY_USER_LIKED,str);
        if(a==null)
            return false;
        int b= Integer.parseInt(a.toString());
        if(b==0)
            return false;
        else
            return true;
    }

    @Override
    public void saveLiked2Redis(Integer likedMessageId, Integer likedPostId) {
        String key = RedisKeyUtils.getLikedKey(likedMessageId, likedPostId);
        redisTemplate.opsForHash().put(RedisKeyUtils.MAP_KEY_USER_LIKED, key, LikedStatusEnum.LIKE.getCode());
    }

    @Override
    public void unlikeFromRedis(Integer likedMessageId, Integer likedPostId) {
        String key = RedisKeyUtils.getLikedKey(likedMessageId, likedPostId);
        redisTemplate.opsForHash().put(RedisKeyUtils.MAP_KEY_USER_LIKED, key, LikedStatusEnum.UNLIKE.getCode());
    }

    @Override
    public void deleteLikedFromRedis(Integer likedMessageId, Integer likedPostId) {
        String key = RedisKeyUtils.getLikedKey(likedMessageId, likedPostId);
        redisTemplate.opsForHash().delete(RedisKeyUtils.MAP_KEY_USER_LIKED, key);
    }

    @Override
    public void incrementLikedCount(Integer likedMessageId) {
        redisTemplate.opsForHash().increment(RedisKeyUtils.MAP_KEY_USER_LIKED_COUNT, likedMessageId, 1);
    }

    @Override
    public void decrementLikedCount(Integer likedMessageId) {
        redisTemplate.opsForHash().increment(RedisKeyUtils.MAP_KEY_USER_LIKED_COUNT, likedMessageId, -1);
    }

    @Override
    public int getLikedMessageValueById(Integer likedMessageId) {
         Object a= redisTemplate.opsForHash().get(RedisKeyUtils.MAP_KEY_USER_LIKED_COUNT,likedMessageId);
         if(a==null)
             return 0;
         else
            return Integer.parseInt(a.toString());
    }

    @Override
    public List<UserLike> getLikedDataFromRedis() {
        Cursor<Map.Entry<Object, Object>> cursor = redisTemplate.opsForHash().scan(RedisKeyUtils.MAP_KEY_USER_LIKED, ScanOptions.NONE);
        List<UserLike> list = new ArrayList<>();
        while (cursor.hasNext()){
            Map.Entry<Object, Object> entry = cursor.next();
            String key = (String) entry.getKey();
            //分离出 likedUserId，likedPostId
            String[] split = key.split("::");
            String likedUserId = split[0];
            String likedPostId = split[1];
            Integer value = (Integer) entry.getValue();

            //组装成 UserLike 对象
            UserLike userLike = new UserLike( (likedUserId == null) ?null:Integer.valueOf(likedUserId), (likedPostId == null) ?null:Integer.valueOf(likedPostId), value);
            list.add(userLike);

            //存到 list 后从 Redis 中删除
            redisTemplate.opsForHash().delete(RedisKeyUtils.MAP_KEY_USER_LIKED, key);
        }

        return list;
    }

    @Override
    public List<LikedCountDTO> getLikedCountFromRedis() {
        Cursor<Map.Entry<Object, Object>> cursor = redisTemplate.opsForHash().scan(RedisKeyUtils.MAP_KEY_USER_LIKED_COUNT, ScanOptions.NONE);
        List<LikedCountDTO> list = new ArrayList<>();
        while (cursor.hasNext()){
            Map.Entry<Object, Object> map = cursor.next();
            //将点赞数量存储在 LikedCountDT
            String key = map.getKey().toString();
            String value=map.getValue().toString();
            LikedCountDTO dto = new LikedCountDTO( (key==null)?null:Integer.valueOf(key) , (value==null)?null:Integer.valueOf(value));
            list.add(dto);
            //从Redis中删除这条记录
//            assert key != null;
            redisTemplate.opsForHash().delete(RedisKeyUtils.MAP_KEY_USER_LIKED_COUNT,Integer.valueOf(key));
        }
        return list;
    }


}